<?php
// 设置允许跨域请求
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: *');

// 检查是否有文件上传
if ($_FILES['image']['error'] === UPLOAD_ERR_OK) {
    // 生成唯一文件名
    $fileName = uniqid() . '_' . $_FILES['image']['name'];
    
    // 设置文件存储路径
    $uploadPath = 'uploads/' . $fileName;

    // 将文件移动到指定目录
    if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadPath)) {
        // 在此处可以将相对地址存储到数据库
        $relativePath = 'uploads/' . $fileName;

        // 返回相对地址给前端
        $response = ['success' => true, 'relativePath' => $relativePath];
        echo json_encode($response);
    } else {
        $response = ['success' => false];
        echo json_encode($response);
    }
} else {
    $response = ['success' => false];
    echo json_encode($response);
}
?>
